package randomsimulation;

/**
 *
 * @author Ganesh Tiwari
 */
public class Random {
    int []r;
    int maxValue;//upper limit
    int a, b;
    int testRange;
    float chiFromTable=17.55f;//TODO: generate chi sq, from fmla .. now supposing
    public Random(int initial,int a,int b, int max,int length){
        r=new int[length];
        testRange=r.length/10;//TODO: correct this
        r[0]=initial;
        this.maxValue=max;
        this.a=a;
        this.b=b;
        System.out.println("end of constructor");
    }
    int [] GenerateRandom(){
        for(int i=1;i<r.length;i++){
            r[i]=(r[i-1]*a+b)%maxValue;
        }
        System.out.println("Random Generation completed");
        return r;
    }
    boolean Test(){
        int n=maxValue/testRange;
        int ei=maxValue/n;
        float chisq;
//        System.out.println("n= "+n+"ei= "+ei);
        int []O=new int[n];
        //count
        for(int i=0;i<r.length;i++){
            for(int j=0;j<n;j++){
                if(j*testRange<=r[i] && (j+1)*testRange>r[i] ){
                    //if withing the range, increase the count
                    O[j]++;
                }
            }
        }
        System.out.println("Range calculation completed");
//        //print O[i]
//        for(int i=0;i<r.length;i++){
//            System.out.println("Random i= "+i+" value : "+r[i]);
//        }
//        for(int i=0;i<n;i++){
//            System.out.println("i= "+i+" value : "+O[i]);
//        }
        //Xo2
        System.out.println("performing chi square");
        float sumsq=0.00f;
        for(int i=0;i<n;i++){
            sumsq+=Math.pow((O[i]-ei), 2);

        }
        //calculate chi sq;
        chisq=sumsq/ei;
        //compare
        if(chisq<=chiFromTable)
            return true;
        else
            return false;
    }
}
